﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using rock.DAL;
using rock.UI.common;
using rock.common;
using rock.BLL;

namespace rock.UI.baseinfo
{
    public partial class UserControlFeeConfig : UserControl
    {
        DalFEE_TYPE dal = new DalFEE_TYPE();
        public UserControlFeeConfig()
        {
            InitializeComponent();
            this.userControlCommonListHeader1.addClick = addClick;
            this.userControlCommonListHeader1.updateClick = updateClick;
            this.userControlCommonListHeader1.deleteClick = deleteClick;
            this.userControlCommonListHeader1.searchClick = searchClick;
            this.Dock = System.Windows.Forms.DockStyle.Fill;
            ViewUtils.setDataGridViewStlye(this.dataGridView1);
        }
        //刷新数据
        public void refreshData()
        {

            String search = this.userControlCommonListHeader1.textBox1.Text;
            DataSet ds = dal.GetList("NAME LIKE '%" + search + "%'");
            this.dataGridView1.DataSource = ds.Tables[0];
        }


        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            updateClick();
        }
        private void addClick()
        {
            FormFeeConfigUpdate fcv = new FormFeeConfigUpdate();
            fcv.oneRefreshParentData = refreshData;
            fcv.ShowDialog(this);
        }
        private void updateClick()
        {
            if (dataGridView1.CurrentRow!=null)
            {
                int row = this.dataGridView1.CurrentRow.Index;
                int col = this.dataGridView1.CurrentCell.ColumnIndex;
                // String value = this.dataGridView1.SelectedRows[row].Cells["CODE_VALUE"].Value.ToString();

                String seqid = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["SEQID"].Value.ToString();

                // this.textBox1.Text = value;
                FormFeeConfigUpdate fcv = new FormFeeConfigUpdate();



                fcv._ModelFEE_TYPE = dal.GetModel(Convert.ToInt32(seqid));
                //注册代理
                fcv.oneRefreshParentData = refreshData;
                fcv.ShowDialog(this);

            }
            else
            {
                MessageBox.Show("请选择要更新的记录.", "提示");
            }
        }
        private void deleteClick()
        {
            if (dataGridView1.CurrentRow!=null)
            {
                String seqid = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["SEQID"].Value.ToString();
                int seqidInt = Convert.ToInt32(seqid);
                if (seqidInt > 0)
                {




                    DialogResult dialogResult = MessageBox.Show("请确认是否要删除！", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

                    if (dialogResult.Equals(DialogResult.OK))
                    {
                        dal.Delete(seqidInt);
                        //刷新数据
                        refreshData();
                    }
                }
            }
            else
            {
                MessageBox.Show("请选择要删除的记录.", "提示");
            }

        }

        private void searchClick()
        {

            refreshData();
        }
        private void UserControlFeeConfig_Load(object sender, EventArgs e)
        {
            refreshData();
        }
         
        #region 费用类型写值
        private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            setFeeTypeDispalyValue(this.dataGridView1);
        }

        public void setFeeTypeDispalyValue(DataGridView dgv)
        {
            for (int a = 0; a < dgv.Rows.Count; a++)
            {

                try
                {
                    int status = (int)dgv.Rows[a].Cells["FEE_MODEL_INT"].Value;
                    //
                    //String name = "";
                    //switch (status)
                    //{
                    //    case Constant.FEE_TYPE_COUNT:
                    //        name = "按使用量计";
                    //        break;
                    //    case Constant.FEE_TYPE_ANCHI:
                    //        name = "按户";
                    //        break;
                    //    case Constant.FEE_TYPE_SIZE:
                    //        name = "按面积";
                    //        break;
                        
                    //    default:
                    //        break;
                    //}
                    DataGridViewTextBoxCell dgvcc = (DataGridViewTextBoxCell)dgv.Rows[a].Cells["FEE_MODEL"];

                    dgvcc.Value = Constant.getByFeeTypeStatus(status).Name;
                    

                }
                catch (Exception)
                {

                }

            }
            // DataGridViewTextBoxCell dgvcc2 = (DataGridViewTextBoxCell)dgv.Rows[1].Cells["NAME"];
            // dgvcc2.Value = "TTTTTTTTTTTTTTT";

        }
        #endregion 费用类型写值

        private void dataGridView1_CellDoubleClick_1(object sender, DataGridViewCellEventArgs e)
        {
            this.updateClick();
        }

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                row.Cells[0].Value = row.Index + 1;
            }
        }

       


    }
}
